package com.hmc.kafka.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * 设置发送消息采用同步还是异步的发送方式
 * send().get()：使用同步：发送到kafka集群后在应答
 * send():使用异步：发送到缓冲区后应答
 */
public class CustomProducerSync {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties properties = CustomProducer.getProperties();
        //创建生产者
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
        //发送数据
        for (int i = 1 ; i<=500 ;i++) {
            //同步发送，当数据被消费完成后在发送新数据
            producer.send(new ProducerRecord<String,String>("first", "hmc"+i)).get();
        }
        //关闭资源
        producer.close();
    }
}
